package com.lry.crawlers.video.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lry.crawlers.video.entity.Video;
import com.lry.crawlers.video.mapper.VideoMapper;
import com.lry.crawlers.video.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class VideoServiceImpl implements VideoService {
    @Autowired
    VideoMapper videoMapper;

    @Override
    public int setVideo(Video video) {
        int insert = videoMapper.insert(video);
        return insert;
    }


    @Override
    public int setVideos(List<Video> videos) {
        int i = videoMapper.insertBatchSomeColumn(videos);
        return i;
    }



    @Override
    public List<Integer> removeDuplicates(List<String> titles) {
        List<Integer> duplicates = new ArrayList<>();
        for(int i = 0;i<titles.size();i++){
            QueryWrapper wrapper  = new QueryWrapper();
            wrapper.eq("title",titles.get(i));
            Video f = videoMapper.selectOne(wrapper);
            if(f!=null){
                duplicates.add(i);
            }
        }
        return  duplicates;
    }

    @Override
    public void setupdateTime(Video video) {
        QueryWrapper wrapper =new QueryWrapper();
        wrapper.eq("video_id",video.getVideoId());
        videoMapper.update(video,wrapper);
    }

    @Override
    public boolean removeDuplicate(String s) {
        QueryWrapper wrapper  = new QueryWrapper();
        wrapper.eq("title",s);
        Video f = videoMapper.selectOne(wrapper);
        return f==null ? false :true;
    }


}


